package com.redteam.data.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * Class providing generic DAO methods.
 *
 * @author Red Team
 * @version 0.9
 */
public class DAOImpl {

    protected static DAOImpl dao;
    protected static DataSource ds;
    protected String tableName;

    static {
        dao = new DAOImpl();
    }

    protected DAOImpl() {
        getDataSource();
    }

    private void getDataSource() {
        try {
            InitialContext ic = new InitialContext();
            ds = (DataSource) ic.lookup("jdbc/redteam");

        } catch (NamingException ex) {
            Logger.getLogger(DAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    /**
     * Sets the table to specified table.
     *
     * @param table table name for particular
     */
    public void setTable(String table) {
        this.tableName = table;
    }

    /**
     * Returns the table name.
     *
     * @return table name
     */
    public String getTable() {
        return this.tableName;
    }

    /**
     * Closes the <code>ResultSet</code>, <code>Statement</code>, and <code>Connection</code> object references.
     *
     * @param rs <code>ResultSet</code> object reference
     * @param st <code>Statement</code> object reference
     * @param conn <code>Connection</code> object reference
     */
    protected void closeConnection(ResultSet rs, Statement st, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException sqle) {
        }
    }
}
